// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// S [author] rhyme_lph
// F [email]  rhymelph@gmail.com
// w [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';
import 'package:flutter/material.dart';

class AmountGstRateCategoryProvider extends BaseProvider {
  TextEditingController amountController = TextEditingController();
  FocusNode amountFocusNode = FocusNode();
  String type;

  SettingDataTax gstRate;

  SettingDataCategory category;

  bool isCheckInclusive = false;

  bool isShowDelete = false;

  bool get enable =>
      amountController.text.length > 0 && gstRate != null && category != null;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    amountController.dispose();
    amountFocusNode.dispose();
  }

  @override
  Future get dataFuture => null;

  void onSelectCategory() async {
    if (isShowDelete == true) {
      final result = await Routes.navigateTo(
          RouterGenProviders.selectCategoryPage,
          params: {
            'isPop': true,
            'select': category,
            'type': type,
          });
      if (result != null) {
        category = result;
        update();
      }
    } else {
      obtainContext?.call((context) {
        Navigator.of(context).pop();
      });
    }
  }

  void onNavDone() {
    if (isShowDelete == true) {
      obtainContext?.call((context) {
        Navigator.of(context)
          ..pop({
            'category': category,
            'gst': gstRate,
            'amount': amountController.text,
            'isCheckInclusive': isCheckInclusive,
          });
      });
    } else {
      obtainContext?.call((context) {
        Navigator.of(context)
          ..pop()
          ..pop({
            'category': category,
            'gst': gstRate,
            'amount': amountController.text,
            'isCheckInclusive': isCheckInclusive,
          });
      });
    }
  }

  void onSelectGstRate() {
    amountFocusNode.unfocus();
    obtainContext?.call((context) async {
      gstRate = await showGstRateBottomSheet(context, gstRate);
      update();
    });
  }

  void onChangeInclusive() {
    isCheckInclusive = !isCheckInclusive;
    update();
  }

  String getGSTInfo() {
    double amount = double.parse(amountController.text, (e) => 0.0);
    double rateDouble = gstRate.rate / 100;
    if (isCheckInclusive == true) {
      double sourceAmount = amount / (1 + rateDouble);
      double inclusive = amount - sourceAmount;
      return CashUtils.formatCashAmount(inclusive);
    } else {
      double gst = amount * rateDouble;
      return CashUtils.formatCashAmount(gst);
    }
  }

  String getTotal() {
    double amount = double.parse(amountController.text, (e) => 0.0);
    double rateDouble = gstRate.rate / 100;
    if (isCheckInclusive == true) {
      return CashUtils.formatCashAmount(amount);
    } else {
      double result = amount + amount * rateDouble;
      return CashUtils.formatCashAmount(result);
    }
  }

  void onDelete() {
    obtainContext?.call((context) {
      Navigator.of(context)
        ..pop({
          'delete': true,
        });
    });
  }

  void initData(SettingDataCategory category, SettingDataTax tax,
      bool isCheckInclusive, String amount) {
    if (category != null) {
      this.category = category;
    }
    if (tax != null) {
      this.gstRate = tax;
    }
    this.isCheckInclusive = isCheckInclusive ?? false;
    if (amount != null) {
      this.isShowDelete = true;
      this.amountController = TextEditingController(text: amount);
    } else {
      amountController = TextEditingController();
    }
    amountController.addListener(() {
      update();
    });
  }
}
